class EventEmitter {
    constructor() {
        this.events = {}
    }
    on(eventName, callback) {
        if (this.events[eventName]) {
            this.events[eventName].push(callback)
        } else {
            this.events[eventName] = [callback]
        }
    }
    emit(eventName) {
        this.events[eventName].forEach(callback => {
            callback()
        })
    }
    off(eventName, callback) {
        if (this.events[eventName]) {
            this.events[eventName] = this.events[eventName].filter(cb => cb !== callback)
        } else {
            console.log(`没有事件:${eventName}`)
        }
    }
    // 一次性订阅
    once(eventName, callback) {
        const wrap = () => {
            callback()
            this.off(eventName, wrap)
        }
        this.on(eventName, wrap)
    }
}
function A() {
    setTimeout(() => {
        console.log('A')
        _event.emit('next')
    }, 1000)
}

function B() {
    setTimeout(() => {
        console.log('B')
        // _event.emit('end')
    }, 1000)
}

A();
let _event = new EventEmitter()
_event.on('next', B)


